package app.blog.entity;

import slap.berkeleydb.BDB;
import app.blog.Blog;

import com.sleepycat.persist.PrimaryIndex;
import com.sleepycat.persist.SecondaryIndex;
import com.sleepycat.persist.model.Entity;
import com.sleepycat.persist.model.PrimaryKey;
import com.sleepycat.persist.model.Relationship;
import com.sleepycat.persist.model.SecondaryKey;


@Entity
public class User {
	
	protected static PrimaryIndex<Integer, User> primaryIndex = 
		BDB.instanse(Blog.bdbName).getEntityStore().getPrimaryIndex(Integer.class, User.class);
	
	protected static SecondaryIndex<String, Integer, User> indexOnName = 
		BDB.instanse(Blog.bdbName).getEntityStore().getSecondaryIndex(primaryIndex, String.class, "name");
	
	public static User getById(int id){
		return primaryIndex.get(id);
	}
	
	public static User getByName(String name){
		return indexOnName.get(name);
	}
	
	public static User persist(User user){
		return primaryIndex.put(user);
	}
	
	@PrimaryKey(sequence = "user-id")
	public int id;
	
	@SecondaryKey(relate = Relationship.ONE_TO_ONE)
	public String name;
	
	public String password;
	
	public String displayName;
	
	public String firstName;
	
	public String lastName;
	
	public String email;
}
